package com.hcbd.tdwt.ui.haige.common.activity;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.ui.fuda.common.fragment.CommunicationFragment_;


import com.hcbd.tdwt.ui.fuda.common.fragment.LocationFragment_;
import com.hcbd.tdwt.ui.haige.common.fragment.BdPositionFragment_;
import com.hcbd.tdwt.ui.haige.common.fragment.MyFragment_;
import com.hcbd.tdwt.ui.haige.common.fragment.SosFragment_;
import com.hcbd.tdwt.MainApp;

import com.hcbd.tdwt.base.AuthManger;
import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.base.BottomBar;
import com.hcbd.tdwt.base.Constant;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.ViewById;

import static com.raizlabs.android.dbflow.config.FlowManager.getContext;

@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_main_haige)
public class MainActivity extends BaseActivity {
    @ViewById
    RelativeLayout mainLL;
    @ViewById
    ImageView sosBtn;
    @ViewById
    BottomBar bottomBar;
    private BdPositionFragment_ bdPositionFragment;
    private CommunicationFragment_ communicationFragment_;
    private MyFragment_ myFragment_;
    private LocationFragment_ locationFragment_;
    private SosFragment_ sosFragment_;

    @AfterViews
    protected void init() {
        bdPositionFragment=new BdPositionFragment_();
        communicationFragment_=new CommunicationFragment_();
        myFragment_=new MyFragment_();
        locationFragment_=new LocationFragment_();
        sosFragment_=new SosFragment_();
        initBottomBar();
        manageExternalPermission();
    }
    private final int  MANAGE_EXTERNAL_REQUEST_CODE=15;
    private void manageExternalPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            // 先判断有没有权限
            if (!Environment.isExternalStorageManager()) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                intent.setData(Uri.parse("package:" + this.getPackageName()));
                startActivityForResult(intent, MANAGE_EXTERNAL_REQUEST_CODE);
            }else{
                checkPermission();
            }
        }else{
            checkPermission();
        }
    }
    public void checkPermission(){
        if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, permissionRequestCode);
        }else{
            AuthManger.getAuthManger().initZipSdk();
            initGps();
        }
    }
    private void initGps() {
        LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            showWarnDialog("请打开手机GPS定位，\n否则APP将不能正常使用", view -> {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, Constant.TO_OPEN_GPS_REQUEST_CODE);
                hideWarnDialog();
            });
        } else {
            //初始化定位sdk
            MainApp.getInstance().initMap();
        }
    }
    public void initBottomBar() {
        bottomBar.setContainer(R.id.fl_container)
                .setFirstChecked(0)
                .setTitleBeforeAndAfterColor("#A9B0BF", "#0068B7")
                .addItem(bdPositionFragment,
                        "卫星", R.drawable.home_bd_pre_haige,
                        R.drawable.home_bd_on_haige)
                .addItem(communicationFragment_,
                        "通讯", R.drawable.home_msg_pre_haige,
                        R.drawable.home_msg_on_haige)
                .addItem(sosFragment_,
                        "", R.drawable.home_sos_haige,
                        R.drawable.home_sos_on_haige)
                .addItem(locationFragment_,
                        "地图", R.drawable.home_map_pre_haige,
                        R.drawable.home_map_on_haige)
                .addItem(myFragment_,
                        "我的", R.drawable.home_my_pre_haige,
                        R.drawable.home_my_on_haige)
                .buildTwo();

        bottomBar.setOnSwitchListener(currentFragment -> {
            if (currentFragment instanceof BdPositionFragment_) {
                ((BdPositionFragment_) bottomBar.getCurrentFragment()).onSwitch();
                setSosHide();
            }else if (currentFragment instanceof CommunicationFragment_) {
                ((CommunicationFragment_) bottomBar.getCurrentFragment()).onSwitch();
                setSosHide();
            }else if (currentFragment instanceof MyFragment_) {
                ((MyFragment_) bottomBar.getCurrentFragment()).onSwitch();
                setSosHide();
            }else  if (currentFragment instanceof LocationFragment_) {
                ((LocationFragment_) bottomBar.getCurrentFragment()).onSwitch();
                setSosHide();
            }else  if (currentFragment instanceof SosFragment_) {
                ((SosFragment_) bottomBar.getCurrentFragment()).onSwitch();
            }

        });
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == permissionRequestCode) {
            AuthManger.getAuthManger().initZipSdk();
            initGps();
            if (bottomBar.getCurrentFragment() instanceof BdPositionFragment_){
                bdPositionFragment.onRequestPermissionsResult(requestCode,permissions,grantResults);
            }
        }
    }
    @OnActivityResult(MANAGE_EXTERNAL_REQUEST_CODE)
    void manageExternalPermissionResult(){
        checkPermission();
    }
    @OnActivityResult(Constant.TO_OPEN_GPS_REQUEST_CODE)
    void OpenGps(){
        hideWarnDialog();
        initGps();
    }
    @Click
    void sosBtn(){
        sosBtn.setBackground(ContextCompat.getDrawable(this,R.drawable.home_sos_on_haige));
        sosBtn.setSelected(true);
        bottomBar.switchFragment(2);
        bottomBar.currentCheckedIndex = 2;
        bottomBar.invalidate();
    }
    public void setSosHide(){
        if(sosBtn.isSelected()){
            sosBtn.setBackground(ContextCompat.getDrawable(this,R.drawable.home_sos_haige));
            sosBtn.setSelected(false);
        }
    }
}
